package com.example.nuc_ms_beta.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.activity.FirstActivity_BG;
import com.example.nuc_ms_beta.util.SharePreferencesUtil;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by bnhc on 17-6-3.
 * Action for Login||Regist
 */

public class FirstActivity extends Activity implements View.OnClickListener {

    private static final int LOGINREQUESTCODE =1;
    private static final int REGISTREQUESTCODE =2;


    private Button activity_first_login, activity_first_regist;
    private LinearLayout activity_first_action, activity_firt_root_view;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x001:
                    Intent intent = new Intent(FirstActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        initView();
        initOnClickListener();
    }

    private void initOnClickListener() {
        activity_first_login.setOnClickListener(this);
        activity_first_regist.setOnClickListener(this);

    }


    /**
     * Init View
     */
    private void initView() {
        activity_first_login = (Button) findViewById(R.id.activity_first_login);
        activity_first_regist = (Button) findViewById(R.id.activity_first_regist);
        activity_first_action = (LinearLayout) findViewById(R.id.activity_first_action);
        activity_firt_root_view = (LinearLayout) findViewById(R.id.activity_firt_root_view);
        //setRootViewBg();

        //isLogin
        Boolean isLogin = (Boolean) SharePreferencesUtil.getData(MyApplication.context, "isLogin", true);
        if (isLogin) {
            //View Visible
            activity_first_action.setVisibility(View.VISIBLE);
        } else {
            activity_first_action.setVisibility(View.GONE);
            //TimerTask
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    Message message = Message.obtain();
                    message.what = 0x001;
                    handler.sendMessage(message);
                }
            };
            timer.schedule(task, 3000);

        }
    }

    /**
     * Set the background
     */
    private void setRootViewBg() {
        BmobQuery<FirstActivity_BG> query_bg = new BmobQuery<FirstActivity_BG>();
        query_bg.findObjects(new FindListener<FirstActivity_BG>() {

            @Override
            public void done(List<FirstActivity_BG> list, BmobException e) {
                Glide.with(MyApplication.context)
                        .load(list.get(0).getFirst_bg().getUrl())
                        .into((ImageView) findViewById(R.id.activity_first_ad_iv));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_first_login:
                Intent loginIntent = new Intent(this, LoginActivity.class);
                startActivityForResult(loginIntent,LOGINREQUESTCODE);
                break;
            case R.id.activity_first_regist:
                Intent registIntent = new Intent(this, RegistActivity.class);
                startActivity(registIntent);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case LOGINREQUESTCODE:
                if(resultCode==LOGINREQUESTCODE) {
                    finish();
                    Intent intent = new Intent(FirstActivity.this, MainActivity.class);
                    startActivity(intent);
                }
                break;
            }
    }
}
